package net.huashitong.Indicators.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.utils.StringUtils;

import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.utils.DateForIndUtils;

@Service
public class GaugeIndustryTotalService {
	@Autowired
	private TertiaryIndustryService industryService;
	@Autowired
	private IndicatorBreakdownService breakdownService;
	@Autowired
	private CompletionClassificationService classificationService;
	
	/**
	 * 规模以上工业产值全市各县区排名情况
	 * @param filter
	 * @return
	 */
	public IndicatorBreakdownDatabean queryCountyRank(
			IndicatorBreakdownFilter filter) {
		List<IndicatorBreakdownDatabean> databeans = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> lists = breakdownService.queryByCuMonthAndAreaOR(filter);
	    if(lists.size()<3){
			return null;
		}
		DecimalFormat df=new DecimalFormat("##.##");
		IndicatorBreakdownDatabean databeans2 = new IndicatorBreakdownDatabean();
		for (IndicatorBreakdown indicatorBreakdown : lists) {
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			databean.setArea(indicatorBreakdown.getArea());
			databean.setRatio(df.format(Double.parseDouble(indicatorBreakdown.getRatio())));
			System.out.println("area="+indicatorBreakdown.getArea());
			System.out.println("Cumulative="+indicatorBreakdown.getCumulative());
			if(StringUtils.isBlank(indicatorBreakdown.getCumulative())||"0".equals(indicatorBreakdown.getCumulative())){
				databean.setCumulative("0");
			}else{
				databean.setCumulative(Double.toString(Double.parseDouble(indicatorBreakdown.getCumulative())/10000).substring(0,4));
			}
		if(indicatorBreakdown.getArea().contains("福州市")){
				databeans.add(0, databean);
			}else if(indicatorBreakdown.getArea().contains("永泰")){
				String st=df.format(Double.parseDouble(indicatorBreakdown.getExpectedTarget())/10000);
				databeans2.setExpectedTarget(st);
				databeans2.setArea(indicatorBreakdown.getArea());
				databeans.add(2, databean);
			}else if(!indicatorBreakdown.getArea().contains("武夷山")&&!indicatorBreakdown.getArea().contains("洛江")){
				databeans.add(databean);
			}
		}
		databeans2.setRightdata(databeans);
		return databeans2;
	}
	/**
	 * 规模以上工业产值当月同比增长情况
	 * @param filter
	 * @return
	 * @throws ParseException
	 */
	public List<IndicatorBreakdownDatabean> queryDevelopmentSituation(
			IndicatorBreakdownFilter filter) throws ParseException {
		List<IndicatorBreakdownDatabean> breakdownDatabean2s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> lists2 = breakdownService.queryMonthDevelopmentSituation(filter);
		
		//校验
		if(lists2.size()<2){
			return null;
		}
		TertiaryIndustry industries = industryService.queryByfilter(filter);
		IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
		String lastDateVal = "";
		String DateVal = "";
		for (IndicatorBreakdown indicatorBreakdown : lists2) {
		if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -1))){
				lastDateVal = indicatorBreakdown.getCumulative();
			}else {
				DateVal = indicatorBreakdown.getCumulative();
				databean.setRatio(indicatorBreakdown.getRatio());
			}
			
		}
		if("".equals(lastDateVal)){
			lastDateVal="0";
		}
		Double double1 = Double.parseDouble(lastDateVal);
		
		if("".equals(DateVal)){
			DateVal="0";
		}
		Double double2 = Double.parseDouble(DateVal);
		Double double3 = double2-double1;
		//查找去年当季和去年当月前一个月数据
		filter.setCurrentMonth(DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(),-12));
		List<IndicatorBreakdown> indicatorBreakdowns = breakdownService.queryMonthDevelopmentSituation(filter);
		for (IndicatorBreakdown indicatorBreakdown : indicatorBreakdowns) {
			if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -1))){
				lastDateVal = indicatorBreakdown.getCumulative();
			}else {
				DateVal = indicatorBreakdown.getCumulative();
			}
		}
		double1 = Double.parseDouble(lastDateVal);
		double2 = Double.parseDouble(DateVal);
		Double double4 = double2-double1;
		double3 =(double3-double4)/double4*100;
		databean.setCountyIncrease(industries.getCountyIncrease());
		databean.setCityIncrease(industries.getCityIncrease());
		if(double3 != 0){
			DecimalFormat df=new DecimalFormat(".##");
			String st=df.format(double3);
			if("0".equals(DateVal)){
				databean.setConcurrentGrowth("0");
			}else{
				databean.setConcurrentGrowth(st);
			}
			
		}
		databean.setuBIncrease(industries.getuBIncrease());
		breakdownDatabean2s.add(databean);
	
		return breakdownDatabean2s;
	}
}
